import 'package:flutter/material.dart';
import '../utils/base.dart';
import 'styles.dart';

import 'package:flutter_picker/flutter_picker.dart';

class XPickerItemBean {
  final int value;

  final String text;

  XPickerItemBean(this.value, this.text);

  @override
  String toString() {
    return text;
  }
}

class XPicker<XPickerItemBean> extends StatefulWidget {
  XPickerItemBean value;
  List<XPickerItemBean> items;
  void Function(XPickerItemBean) onChanged;

  XPicker({Key key, this.value, this.items, this.onChanged}) : super(key: key);

  _XPickerState createState() => _XPickerState(value, items, onChanged);
}

class _XPickerState<XPickerItemBean> extends State<XPicker> {
  XPickerItemBean value;
  List<XPickerItemBean> items;
  void Function(XPickerItemBean) onChanged;

  _XPickerState(this.value, this.items, this.onChanged);

  @override
  Widget build(BuildContext context) {
    BorderRadius _borderRadius =
        BorderRadius.all(Radius.circular(Design.px(17)));

    return Container(
      margin: EdgeInsets.symmetric(horizontal: Design.px(13)),
      width: Design.px(400),
      height: Design.px(80),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: _borderRadius,
      ),
      child: FlatButton(
        shape: RoundedRectangleBorder(borderRadius: _borderRadius),
        onPressed: () => new Picker(
            adapter: PickerDataAdapter<String>(pickerdata: items),
            changeToFirst: true,
            hideHeader: false,
            onConfirm: (Picker picker, List value) {
              setState(() {
                String selectedText = picker.getSelectedValues()[0].toString();
                for (var i = 0; i < items.length; i++) {
                  XPickerItemBean tt = items[i];
                  if (tt.toString() == selectedText) {
                    this.value = tt;
                  }
                }
              });
            }).showModal(this.context),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Container(
                child: Text(
                  value.toString(),
                  maxLines: 1,
                  style: TextStyle(
                      fontSize: Design.px(30), color: HexColor("333333")),
                ),
              ),
            ),
            Image.asset(
              'assets/icon-dropdown.png',
              height: Design.px(18),
            ),
          ],
        ),
      ),
    );
  }
}
